/** ***************************************************************************
 * CoffeeAllocator.hpp
 * 2012 CoffeeParty Ltd.
 * Date Created: 2012-07-21                                  format YYYY-MM-DD
 * Revision Id: $Id$
 ******************************************************************************
 */

#ifndef _COFFEE_ALLOCATOR_HPP_
#define _COFFEE_ALLOCATOR_HPP_

#include "CoffeeCommons.hpp"
#include <new>
#include <memory>

template<typename T>
class CoffeeAllocator
{
public:
    CoffeeAllocator() { }
    ~CoffeeAllocator() { }

    T* allocate(size_t cnt)
    {
        return (T*)internal_new(cnt * sizeof(T));
    }

    void deallocate(T* ptr)
    {
        internal_delete(ptr);
    }

    void construct(T* ptr, const T&e)
    {
        new ((void*)ptr) T(e);
    }

    void destruct(T* ptr)
    {
        ptr->~T();
    }

private:
    virtual void* internal_new(size_t cnt)
    {
        return operator new(cnt);
    }

    virtual void internal_delete(void* ptr)
    {
        operator delete(ptr);
    }
};

#endif /** _COFFEE_ALLOCATOR_HPP_ */

